package com.crm.finance.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizFinanceCostclass;
import com.crm.model.BizProject;
import com.crm.model.FinanceZpIncome;
import com.crm.service.BizFinanceCostclassService;
import com.crm.service.BizProjectService;
import com.crm.service.FinanceZpIncomeService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/financeZpIncome")
public class FinanceZpIncomeController {
	@Autowired
	private  FinanceZpIncomeService financeZpIncomeService;
	
	@Autowired
	private BizProjectService projectService;
	
	@Autowired
	private BizFinanceCostclassService costclassService;
	
	
	@RequestMapping("/index")
	public String index(){
		return "financeCenter/zpIncome";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<FinanceZpIncome> page, FinanceZpIncome t){
		
		Page<FinanceZpIncome> list = (Page<FinanceZpIncome>) financeZpIncomeService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(FinanceZpIncome record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpIncomeService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(FinanceZpIncome record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpIncomeService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(FinanceZpIncome record){
		
		Map<String, Object> map = new HashMap<>();
		
		financeZpIncomeService.deleteById(record.getZpIncomeId());
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/showProject")
	@ResponseBody
	public void showProject(HttpServletRequest request,HttpServletResponse response){
		
		Page<BizProject> page = new Page<BizProject>();
		
		List<BizProject> list = projectService.selectAll(page);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/showPCostclass")
	@ResponseBody
	public void showPCostclass(HttpServletRequest request,HttpServletResponse response){
		
		Page<BizFinanceCostclass> page = new Page<BizFinanceCostclass>();
		
		BizFinanceCostclass costclass = new BizFinanceCostclass();
		
		costclass.setParentId("0");
		
		List<BizFinanceCostclass> list = costclassService.selectAll(page, costclass);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/showCostclass")
	@ResponseBody
	public void showCostclass(HttpServletRequest request,HttpServletResponse response,String financeCostclassId){
		
		Page<BizFinanceCostclass> page = new Page<BizFinanceCostclass>();
		
		BizFinanceCostclass costclass = new BizFinanceCostclass();
		
		costclass.setParentId(financeCostclassId);
		
		List<BizFinanceCostclass> list = costclassService.selectAll(page, costclass);
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	@RequestMapping("/getTotalIncome")
	@ResponseBody
	public void getTotalIncome(HttpServletResponse response,HttpServletRequest request,FinanceZpIncome t){
		FinanceZpIncome income = financeZpIncomeService.getTotalIncome(t);
		ResponseUtil.result(response, JsonUtil.toJsonStr(income));
	}
}
